<?php

namespace app\admin\model;

/**
 * 默认配置-模型
 * @author ZhangXuanNeng
 * @since 2024/05/23
 * Class DefaultConfig
 * @package app\admin\model
 */
class DefaultConfig extends BaseModel
{
    /**
     * 获取指定配置
     * @param string $name 配置名
     */
    public function getDefaultConfig($name){
        $row = $this->where('name',$name)->find();
        $data = json_decode($row['value'],true);
        return $data;
    }
    /**
     * 保存配置
     * @param array $data 保存数据
     */
    public function setDefaultConfig($data){
        $name = $data['name'];
        unset($data['name']);
        $row = $this->where('name',$name)->find();
        if($row){
            $oldOptionValue = json_decode($row['value'],true);
            if (!empty($oldOptionValue)) {
                $data = array_merge($oldOptionValue, $data);
            }
            $res = $this->where('name',$name)->update(['value'=>json_encode($data)]);
        }else{
            $option['name']  = $name;
            $option['value'] = json_encode($data);
            $res = $this->insert($option);
        }
        $msg = array(
            'code'  => 0,
            'msg'   => '操作成功'
        );
        if(!$res){
            $msg = array(
                'code'  => -1,
                'msg'   => '操作失败'
            );
        }
        return $msg;
    }
}
